import java.io.IOException;

import javax.imageio.ImageIO;


public class PigTarget extends MovingTarget {

	private static final int DAMAGE_TAKEN_BY_COLIBRIS = 1;
	private static final int DAMAGE_TAKEN_BY_MOINEAUX = 1;
	private static final int DAMAGE_TAKEN_BY_PIGEON   = 1;
	private static final int POINTS_GIVEN_WHEN_KILLED = 20;
	private static final int LIFE_POINTS_DEFAULT = 1;

	private static final String IMG_URL = "Ressources/target.png";

	public PigTarget(int posX, int posY) {
		this.loadRessources();
		this.init(posX,posY);
	}

	public void init(int posX, int posY) {
		this.lifePoints = LIFE_POINTS_DEFAULT;
		this.isLiving = true;
		super.init(posX, posY);
	}

	public void receiveDamage(int type) {
		int nb_damage = 0;
		if(type == Bird.COLIBRIS_TYPE)
			nb_damage = DAMAGE_TAKEN_BY_COLIBRIS;
		if(type == Bird.MOINEAUX_TYPE)
			nb_damage = DAMAGE_TAKEN_BY_MOINEAUX;
		if(type == Bird.PIGEON_TYPE)
			nb_damage = DAMAGE_TAKEN_BY_PIGEON;

		this.lifePoints -= nb_damage;
		if(this.lifePoints <= 0)
			this.setDead();
	}

	private void loadRessources() {
		try {
			this.loadExplosionImg();
			this.image = ImageIO.read((getClass().getResource(IMG_URL)));
			this.imageHeight = this.image.getHeight(null);
			this.imageWidth = this.image.getWidth(null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void update(Map map){
		if(!this.isExploding()){
			super.nextMove(map);
		}
		else if(!this.hasExplode()){
			this.updateExplosion();
		}
	}

	@Override
	protected int getKillingPoints() {
		return POINTS_GIVEN_WHEN_KILLED;
	}

	@Override
	protected void setDead() {
		this.isLiving = false;
	}

	@Override
	protected void loadExplosionImg() {
		try {
			for(int i=0; i<this.imageArrayExplosion.length; ++i){
				this.imageArrayExplosion[i] = ImageIO.read((getClass().getResource("Ressources/explo_20_px_"+i+".png")));
			}
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
